package com.my.study.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.my.study.exception.BusinessException;
import com.my.study.enums.BusinessExceptionEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class JsonUtil {

    private static ObjectMapper mapper = new ObjectMapper();

    static {
        // 属性为NULL 不序列化
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 属性为 空（""） 或者为 NULL 都不序列化
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        // 属性为默认值不序列化 (例如int为0，Object为null)
        mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
    }

    /**
     * 对象转json
     *
     * @param obj
     * @return
     */
    public static String objectToString(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException exception) {
            log.error("json exchange error" + exception);
            throw new BusinessException(BusinessExceptionEnum.FAIL.code, "json转换失败");
        }
    }

    /**
     * map转json
     *
     * @param map
     * @return
     */
    public static String mapToJson(Map<String, Object> map) {
        try {
            return mapper.writeValueAsString(map);
        } catch (JsonProcessingException exception) {
            log.error("json exchange error" + exception);
            throw new BusinessException(BusinessExceptionEnum.FAIL.code, "json转换失败");
        }
    }

    /**
     * json转实体（针对属性为string，内置对象不变，与beanUtils类似）
     *
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T jsonToObject(String json, Class<T> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (JsonProcessingException exception) {
            log.error("json exchange error" + exception);
            throw new BusinessException(BusinessExceptionEnum.FAIL.code, "json转换失败");
        }
    }

    /**
     * json转object(内置对象也转换)
     *
     * @param json
     * @param valueType
     * @param <T>
     * @return
     */
    public static <T> T jsonToObject(String json, TypeReference<T> valueType) {
        try {
            return mapper.readValue(json, valueType);
        } catch (JsonProcessingException exception) {
            log.error("json exchange error" + exception);
            throw new BusinessException(BusinessExceptionEnum.FAIL.code, "json转换失败");
        }
    }

    /**
     * object to Target
     *
     * @param object
     * @param valueType
     * @param <T>
     * @return
     */
    public static <T> T ObjectToTarget(Object object, Class<T> valueType) {
        try {
            return mapper.convertValue(object, valueType);
        } catch (Exception exception) {
            log.error("Entity conversion failed" + exception);
            throw new BusinessException(BusinessExceptionEnum.FAIL.code, "实体转换失败");
        }
    }

    /**
     * object to Entity
     *
     * @param object
     * @param valueType
     * @param <T>
     * @return
     */
    public static <T> T ObjectToEntity(Object object, TypeReference<T> valueType) {
        try {
            return mapper.convertValue(object, valueType);
        } catch (Exception exception) {
            log.error("Entity conversion failed" + exception);
            throw new BusinessException(BusinessExceptionEnum.FAIL.code, "实体转换失败");
        }
    }
}
